package dm.ast;

import com.sun.xml.internal.bind.annotation.OverrideAnnotationOf;
import dm.ast.model.gen.DmConstantGen;
import dm.ast.pro.DmCastFun;
import dm.repository.IFunction;
import test.envset.GlobalState;
import test.utility.Randomly;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weihuixin
 * @date 2024/6/13
 * @Description
 */
public class DmIFun extends DmExpression {

    public ArrayList<DmExpression> iFunParas = new ArrayList<>();
    public String iFunName = null;

    public void setIFunName(String iFunName) {
        this.iFunName = iFunName;
    }

    @Override
    public void fill(GlobalState globalState) {
        if (!isCompleted) {
            IFunction iFun = globalState.getRanIFun(reType);
            if(iFun != null){
                setIFunName(iFun.funName);
                ArrayList<DmDataType> paras = iFun.paras;
                for (int i = 0; i < paras.size(); i++) {
                    DmExpression tmp = globalState.getRanCol(paras.get(i));
                    tmp.fill(globalState);
                    iFunParas.add(tmp);
                }
                isCompleted = true;
            }

        }
    }

    @Override
    public String toSql() {
        StringBuilder sql = new StringBuilder();
        sql.append(iFunName + "(");
        for (int i = 0; i < iFunParas.size(); i++) {
            if(i == (iFunParas.size() - 1)){
                sql.append(iFunParas.get(i).toSql());
                break;
            }
            sql.append(iFunParas.get(i).toSql() + ",");
        }
        sql.append(")");
        return sql.toString();
    }
}
